<?php

class status extends base {

    private $content;
    private $maxlogsize = 32000;

    public function submenu() {
        $this->submenu = array();
        $this->submenu[0]["name"] = "Accel logs";
        $this->submenu[0]["callback"] = "accellogs";
        $this->submenu[0]["icon"] = "icons/cross.png";
        $this->submenu[0]["rights"] = 0;
        $this->submenu[0]["enabled"] = true;
        $this->submenu[1]["name"] = "Accel errors";
        $this->submenu[1]["callback"] = "accelerrors";
        $this->submenu[1]["icon"] = "icons/cross.png";
        $this->submenu[1]["rights"] = 0;
        $this->submenu[1]["enabled"] = true;
        $this->submenu[2]["name"] = "Accel warnings";
        $this->submenu[2]["callback"] = "accelwarnings";
        $this->submenu[2]["icon"] = "icons/cross.png";
        $this->submenu[2]["rights"] = 0;
        $this->submenu[2]["enabled"] = true;
        $this->submenu[3]["name"] = "Accel fail";
        $this->submenu[3]["callback"] = "accelfails";
        $this->submenu[3]["icon"] = "icons/cross.png";
        $this->submenu[3]["rights"] = 0;
        $this->submenu[3]["enabled"] = true;
        $this->submenu[4]["name"] = "Accel core";
        $this->submenu[4]["callback"] = "accelcore";
        $this->submenu[4]["icon"] = "icons/cross.png";
        $this->submenu[4]["rights"] = 0;
        $this->submenu[4]["enabled"] = true;
        $this->submenu[5]["name"] = "Accel emergency";
        $this->submenu[5]["callback"] = "accelemergency";
        $this->submenu[5]["icon"] = "icons/cross.png";
        $this->submenu[5]["rights"] = 0;
        $this->submenu[5]["enabled"] = true;
        $this->submenu[6]["name"] = "Expired log";
        $this->submenu[6]["callback"] = "expiredlog";
        $this->submenu[6]["icon"] = "icons/cross.png";
        $this->submenu[6]["rights"] = 0;
        $this->submenu[6]["enabled"] = true;
        $this->submenu[7]["name"] = "Users log";
        $this->submenu[7]["callback"] = "userslog";
        $this->submenu[7]["icon"] = "icons/cross.png";
        $this->submenu[7]["rights"] = 0;
        $this->submenu[7]["enabled"] = true;
        $this->submenu[8]["name"] = "Kernel log";
        $this->submenu[8]["callback"] = "kernellog";
        $this->submenu[8]["icon"] = "icons/cross.png";
        $this->submenu[8]["rights"] = 0;
        $this->submenu[8]["enabled"] = true;
        $this->submenu[9]["name"] = "Limits";
        $this->submenu[9]["callback"] = "limits";
        $this->submenu[9]["icon"] = "icons/cross.png";
        $this->submenu[9]["rights"] = 0;
        $this->submenu[9]["enabled"] = true;

        $this->_submenu($this->submenu);
    }

    public function run() {
        $this->submenu();
        $callback = $this->_run();
        if ($callback)
            $this->content = $this->$callback();
        $this->finish($this->content);
    }

    public function getlastbytes($filename, $bytes) {
        $fh = fopen($filename, "r");
        if (!$fh)
            return false;
        fseek($fh, 0 - $bytes, SEEK_END);
        $buffer = fread($fh, $bytes);
        fclose($fh);
        return $buffer;
    }

    public function kernellog() {
        $kern_log = $this->getlastbytes("/var/log/kern.log", 5000);
        $kern_log = str_replace("\n", "<br>", $kern_log);
        return $kern_log;
    }

    public function limits() {
        $r = file_get_contents('dumps/limits');
        $ra = explode("\n", $r);
        foreach ($ra as $rai) {
            if (strstr($rai, "TcpExt: SyncookiesSent")) {
                $fin.="<table border=1><tr><td>";
                $fin.=str_replace(" ","</td><td>",$rai);
                $fin.="</td></tr>";
            } else
            if (strstr($rai, "TcpExt:")) {
                $fin.="<tr><td>";
                $fin.=str_replace(" ","</td><td>",$rai);
                $fin.="</td></tr></table><br><br>";
            } else
            if (strstr($rai, "IpExt: InNoRoutes")) {
                $fin.="<table border=1><tr><td>";
                $fin.=str_replace(" ","</td><td>",$rai);
                $fin.="</td></tr>";
            } else
            if (strstr($rai, "IpExt: ")) {
                $fin.="<tr><td>";
                $fin.=str_replace(" ","</td><td>",$rai);
                $fin.="</td></tr></table><br><br>";
            } else
            $fin.=$rai."<br>\n";
        }
        return $fin;
    }

    public function expiredlog() {
        $exp_log = $this->getlastbytes("dumps/checklog", 5000);
        $exp_log = str_replace("\n", "<br>", $exp_log);
        return $exp_log;
    }

    public function accelfails() {
        $auth_fail_log = $this->getlastbytes("/var/log/accel-ppp/auth-fail.log", $this->maxlogsize);
        $auth_fail_loga = explode("\n", $auth_fail_log);
        foreach ($auth_fail_loga as $acla) {
            if (strstr($acla, "authentication failed"))
                $auth_fail_log_color.="<font color='red'>{$acla}</font><br>\n";
            else
            if (strstr($acla, "You are already logged in - access denied"))
                $auth_fail_log_color.="<font color='blue'>{$acla}</font><br>\n";
            else
            if (strstr($acla, "connect:"))
                $auth_fail_log_color.="<font color='green'>{$acla}</font><br>\n";
            //else
            //$auth_fail_log_color.=$acla . "<br>\n";
        }
        //$auth_fail_log = $this->getlastbytes("/var/log/accel-ppp/auth-fail.log", 5000);
        //$auth_fail_log = substr($auth_fail_log, strlen($auth_fail_log) - 5000);
        $auth_fail_log = "{$auth_fail_log_color}<br><br>\n";
        return $auth_fail_log;
    }

    public function accelerrors() {
        exec("cat /var/log/accel-ppp/accel-ppp.log | grep error", $lines);
        foreach ($lines as $line)
            $output.=$line . "<br>" . PHP_EOL;
        return $output;
    }

    public function accelwarnings() {
        exec("cat /var/log/accel-ppp/accel-ppp.log | grep warn", $lines);
        foreach ($lines as $line)
            $output.=$line . "<br>" . PHP_EOL;
        return $output;
    }

    public function accelcore() {
        $core_log = file_get_contents("/var/log/accel-ppp/core.log");
        $core_log = str_replace("\n", "<br>", $core_log) . "<br><br>\n";
        return $core_log;
    }

    public function accelemergency() {
        $emerg_log = file_get_contents("/var/log/accel-ppp/emerg.log");
        $emerg_log = str_replace("\n", "<br>", $emerg_log) . "<br><br>\n";
        return $emerg_log;
    }

    public function accellogs() {
        $accel_log = $this->getlastbytes("/var/log/accel-ppp/accel-ppp.log", 5000);
        $accel_loga = explode("\n", $accel_log);
        foreach ($accel_loga as $acl) {
            if (strstr($acl, "authentication failed"))
                $accel_log_color.=$acl . "<br>\n"; else
                $accel_log_color.=$acl . "<br>\n";
        }
        $accel_log = "Accel logs: <br/>{$accel_log_color}<br><br>\n";

        return $accel_log;
    }

    private function userslog_file($filename) {
        $filename = "/var/log/accel-ppp/users/" . $filename;
        if (!file_exists($filename))
            return "No file {$filename}!";
        $fc = file_get_contents($filename);
        $fc = str_replace("\n", "<br>\n", $fc);
        return $fc;
    }

    public function userslog() {
        $entry = $_GET["entry"];
        if ($entry)
            return $this->userslog_file($entry);
        $dir = scandir("/var/log/accel-ppp/users");
        sort($dir);
        foreach ($dir as $entry) {
            $fs = filesize("/var/log/accel-ppp/users/" . $entry);
            if ($entry != "." && $entry != "..")
                $output.="<a href='index.php?page=status&submenu=Users%20log&entry={$entry}'>{$entry}</a> {$fs} bytes<br>\n";
        }

        return $output;
    }

}

?>